package com.pine.dcts.handler;

import com.pine.dcts.common.CURDHandler;
import com.pine.dcts.common.Result;
import com.pine.dcts.entity.DatasourceInfo;
import com.pine.dcts.model.Field;
import com.pine.dcts.model.OrgDTO;
import com.pine.dcts.model.Table;
import com.pine.dcts.service.DatasourceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

@RequestMapping("/datasource")
@RestController
@Slf4j
@Validated
public class DatasourceHandler extends CURDHandler<DatasourceService, DatasourceInfo> {


    @GetMapping("/getDatasourceTree")
    public Result<List<OrgDTO>> getDatasourceTree() {
        return success(this.service.getDatasourceTree());
    }

    @GetMapping("/getTablesByDatasourceId")
    public Result<List<Table>> getTablesByDatasourceId(@NotNull(message = "数据源id不能为空") Integer datasourceId,String schemaName) {
        return success(this.service.getTablesByDatasourceId(datasourceId,schemaName));
    }

    @GetMapping("/getSchemasByDatasourceId")
    public Result<List<Table>> getSchemasByDatasourceId(@NotNull(message = "数据源id不能为空") Integer datasourceId) {
        return success(this.service.getSchemasByDatasourceId(datasourceId));
    }

    @GetMapping("/getFieldsByTable")
    public Result<List<Field>> getFieldsByTable(@NotNull(message = "数据源id不能为空") Integer datasourceId, String schemaName, @NotBlank(message = "表名称不能为空") String tableName) {
        return success(this.service.getFieldsByTable(datasourceId,schemaName,tableName));
    }

}
